package com.tedu.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tedu.model.Address;
import com.tedu.model.Product;
import com.tedu.model.User;
import com.tedu.service.IAddressService;
import com.tedu.service.IUserService;
import com.tedu.util.CodeUtil;

@Controller
@RequestMapping("/user")
public class UserController {
	private IUserService userService;
	private IAddressService addressService;

	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Resource
	public void setAddressService(IAddressService addressService) {
		this.addressService = addressService;
	}

	/**
	 * 跳转到登录页面
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		return "client/login";
	}

	/**
	 * 用户登录登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(String username, String password, HttpSession session) {
		User user = userService.login(username, password);
		session.setAttribute("loginUser", user);
		return "redirect:/index.html";
	}

	/**
	 * 跳转到注册页面
	 */
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register(Model model) {
		int status = 0;
		model.addAttribute(new User());
		return "client/register";
	}

	/**
	 * 用户注册方法
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String register(User user) {
		userService.add(user);
		return "redirect:/user/login";
	}

	/**
	 * 退出登录 王敬雯
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.invalidate();
		return "client/login";
	}

/////////////////////////////////////////////////////////////////////// 新项目
	/**
	 * 跳转到信息修改页
	 */
	@RequestMapping(value = "/personal_imformation", method = RequestMethod.GET)
	public String personal_imformation(Model model, HttpSession session) {
		User user = (User) session.getAttribute("loginUser");
		model.addAttribute("loginUser", user);
		return "client/personal_imformation";
	}

	/**
	 * 修改信息
	 */
	@RequestMapping(value = "/imformationAlter", method = RequestMethod.PUT)
	public void imformationSAlter(User user, HttpSession session, HttpServletResponse resp,
			HttpServletRequest request) {
		Writer out = null;
		resp.setContentType("text/html;charset=utf-8");
		userService.update(user);
		session.setAttribute("loginUser", user);
		try {
			out = resp.getWriter();
			out.write("<script language='javascript'>alert('个人修改成功!');window.location.href='" + request.getContextPath()
					+ "/user/personal_imformation';</script>");
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到密码修改页
	 */
	@RequestMapping(value = "/password_imformation", method = RequestMethod.GET)
	public String password_imformation(Model model, HttpSession session) {
		User user = (User) session.getAttribute("loginUser");
		model.addAttribute("loginUser", user);
		return "client/passwordAlter";
	}

	/**
	 * 修改密码
	 */
	@RequestMapping(value = "/passwordAlter", method = RequestMethod.PUT)
	public void passwordAlter(String oldPassword, String newPassword, HttpSession session, HttpServletResponse resp,
			HttpServletRequest request) {
		resp.setContentType("text/html;charset=utf-8");
		User loginUser = (User) session.getAttribute("loginUser");
		Writer out = null;
		if (loginUser != null) {
			if (CodeUtil.getMD5Encoding(oldPassword).equals(loginUser.getPassword())) {
				userService.passwordAlter(CodeUtil.getMD5Encoding(newPassword), loginUser.getId());
				session.removeAttribute("loginUser");
				try {
					out = resp.getWriter();
					out.write("<script language='javascript'>alert('密码修改成功!');window.location.href='"
							+ request.getContextPath() + "/index.html';</script>");
				} catch (IOException e) {
// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					out = resp.getWriter();
					out.write("<script language='javascript'>alert('旧密码验证错误!');window.location.href='"
							+ request.getContextPath() + "/user/password_imformation';</script>");
				} catch (IOException e) {
// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}

	/**
	 * 跳转到地址页
	 */
	@RequestMapping(value = "/address_imformation", method = RequestMethod.GET)
	public String address_imformation(Model model, HttpSession session) {
		User user = (User) session.getAttribute("loginUser");
		if (user != null) {
			List<Address> commomList = addressService.commomList(user.getId());
			List<Address> defaultList = addressService.defaultList(user.getId());
			model.addAttribute("commomList", commomList);
			model.addAttribute("defaultList", defaultList);
			return "client/addressAlter";
		} else {
			return null;
		}

	}

	/**
	 * 提交新地址信息
	 */
	@RequestMapping(value = "/address_add", method = RequestMethod.POST)
	public String address_add(Address address, HttpSession session) {
		address.setBase(address.getBase().replace("/", ""));
		System.out.println("基本地址：" + address.getBase());
		User user = (User) session.getAttribute("loginUser");
		address.setUser(user);
		if (address.getStatus() == 0) {
			addressService.updateAllStatus();
			addressService.add(address);
		} else {
			addressService.add(address);
		}
		return "redirect:/user/address_imformation";
	}

	/**
	 * 跳转到地址修改回显页
	 */
	@RequestMapping(value = "/{id}/addressEcho", method = RequestMethod.GET)
	public String addressEcho(@PathVariable int id, Model model) {
		Address address = addressService.load(id);
		model.addAttribute("address", address);
		return "client/addressEcho";
	}

	/**
	 * 提交修改后的地址
	 */
	@RequestMapping(value = "/addressAlter", method = RequestMethod.PUT)
	public String addressAlter(Address address, Model model) {
		address.setBase(address.getBase().replace("/", ""));
		if (address.getStatus() == 0) {
			addressService.updateAllStatus();
			addressService.update(address);
		} else {
			addressService.update(address);
		}
		return "redirect:/user/address_imformation";
	}

	/**
	 * 删除地址
	 */
	@RequestMapping(value = "/{id}/del", method = RequestMethod.DELETE)
	public String addressDelete(@PathVariable int id) {
		addressService.delete(id);
		return "redirect:/user/address_imformation";
	}

	/**
	 * 设为默认
	 */
	@RequestMapping(value = "/{id}/setDefault", method = RequestMethod.GET)
	public String setDefault(@PathVariable int id) {
		addressService.setDefaule(id);
		return "redirect:/user/address_imformation";
	}

}
